import { tools, compute } from "./tools.js";
export default class Calculator {
  constructor() {
    this.oCalculator = document.querySelector(".calculator");
    this.oResult = this.oCalculator.querySelector(".result");
    this.fInput = this.oCalculator.querySelectorAll("input")[0];
    this.sInput = this.oCalculator.querySelectorAll("input")[1];
    this.oBtnGroup = this.oCalculator.querySelector(".btn-group");
  }

  init() {
    this.bindEvent();
  }
  bindEvent() {
    this.oBtnGroup.addEventListener("click", this.onBtnClick.bind(this), false);
  }
  onBtnClick(ev) {
    const tar = tools.getTarget(ev),
      tagName = tar.tagName.toLowerCase();
    if (tagName === "button") {
      let method = tar.getAttribute("data-method"),
        fVal = tools.digitalize(this.fInput.value),
        sVal = tools.digitalize(this.sInput.value);

      this.renderResult(compute[method](fVal, sVal));
    }
  }
  //渲染结果
  renderResult(result) {
    this.oResult.innerHTML = result;
  }
}
